package com.zmj.solr.controller;

import com.alibaba.fastjson.JSONArray;
import com.zmj.solr.common.GenericController;
import com.zmj.solr.common.Result;
import com.zmj.solr.service.IsolrService;
import com.zmj.solr.util.FileUtils;
import com.zmj.solr.vo.SolrProduct;
import com.zmj.solr.vo.SuggestConf;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 周美军
 * @Email: 536304123@QQ.COM
 */
@Controller
@RequestMapping(value = "/solr")
public class SolrController extends GenericController {

    @Autowired
    private IsolrService isolrService;

    /**
     * 根据search关键词查询
     * @param response
     * @param search
     * @param sort
     * @param sortField
     * @param productCateId
     * @param cId
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public void query(HttpServletResponse response, String search, String sort, String sortField, Integer productCateId, String cId) {
        Map<String, Object> query = null;
        try {
            Map map = new HashMap<String, Object>();
            map.put("keyword", search);
            map.put("sort", sort);
            map.put("sortField", sortField);
            map.put("productCateId", productCateId);
            map.put("cId", cId);
            query = isolrService.query(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderSuccessString(response, query);
    }

    /**
     * 初始化索引数据
     * @return
     */
    @RequestMapping(value = "/createIndex", method = RequestMethod.GET)
    @ResponseBody
    public Result createIndex() {
        return renderSuccessString(isolrService.createIndex());
    }

    /**
     * 删除全部索引
     * @return
     */
    @RequestMapping(value = "/deleteAll", method = RequestMethod.GET)
    @ResponseBody
    public Result deleteAll() {
        return  renderSuccessString(isolrService.deleteAll());
    }


    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ResponseBody
    public Result deleteById(String id) {
        return  renderSuccessString(isolrService.deleteById(id));
    }

    /**
     * 联想搜索
     * @param search
     * @return
     */
    @RequestMapping(value = "/suggest", method = RequestMethod.GET)
    @ResponseBody
    public Result suggest(String search) {
        return renderSuccessString(isolrService.suggest(search));
    }

    /**
     * 使联想搜索字典表即时生效
     * @param search
     * @return
     */
    @RequestMapping(value = "/build", method = RequestMethod.GET)
    @ResponseBody
    public Result buildConf(String search) {
        return renderSuccessString(isolrService.buildSuggestConf());
    }

    /**
     * 修改suggest dict.txt联想搜索文件
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Result writeSuggestConf( @RequestBody List<SuggestConf> confs,@RequestParam(defaultValue = "false") boolean append) {
        //Postman请求 设置 Content-Type : application/json   Accept : application/json
        //body请求raw
        //[{"name":"华为是伟大的","weight":2.4},{"name":"中华人民共和国","weight":4.5},{"name":"中华英雄","weight":3.6}]
        return renderSuccessString(isolrService.writeSuggestConf(confs,append,"zmj"));
    }

    /**
     * 修改suggest dict.txt联想搜索文件
     * @param file
     * @param append
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Result writeSuggestConfFile(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file,
                                       @RequestParam(defaultValue = "false") boolean append) {
        String msg = FileUtils.check(file);
        if (StringUtils.isNotBlank(msg)){
            return renderErrorString(msg);
        }

        String basePath = request.getSession().getServletContext().getRealPath("/");
        File tempFile = com.zmj.solr.util.FileUtils.mkdir(basePath,"/upload//");

        return renderSuccessString(isolrService.writeSuggestConfFile(tempFile,file,append,null));
    }

    //http://localhost:8081/springmvc_solr_demo_war_exploded/solr/test
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void test(HttpServletResponse response, SolrProduct item) {
        renderSuccessString(response, "hello,solr");
    }

}
